package com.adxon.filter;

import com.adxon.MsgException;
import com.adxon.servlet.ServletBase;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FilterBase implements Filter{
	private FilterConfig cfg;
	protected ServletContext ctx;

	public void destroy(){
	}
	public void doFilter(ServletRequest req,ServletResponse resp, 
			FilterChain chain)throws IOException,ServletException {
		if(req instanceof HttpServletRequest){
			HttpServletRequest rq=(HttpServletRequest)req;
			HttpServletResponse rs=(HttpServletResponse)resp;
			if(rq.getMethod().equals("GET")){
				System.out.println("FilterBase: "+rq.getRequestURI());
				try{
					doGet(rq,rs,chain);
					return;
				}catch(IllegalAccessException e){
					e.printStackTrace();
				}catch(MsgException e){
					if(e.getCode()!=MsgException.NOT_FOUND){
						e.routing(rq,rs);
						return;}
				}catch(InstantiationException e){
					e.printStackTrace();
					}
				}
			}
		chain.doFilter(req,resp);
	}
	public void doGet(HttpServletRequest req,HttpServletResponse resp,
			FilterChain chain)throws IllegalAccessException,
			InstantiationException,IOException,MsgException,ServletException{
	}
	public FilterConfig getFilterConfig(){
		return cfg;
	}
	public void init(FilterConfig config){
		cfg=config;
		ctx=config.getServletContext();
		ServletBase.init(ctx);
	}
}